<?php


class ParkDAO {
   
    function __construct($db) {
        $this->db=$db;
    }
    
    
     public function create(ContactPark $park) {
		$db = $this->db;
                
$stmt = $db->prepare("INSERT INTO park (clients ,car ,datein ,dateout ,status ,fare ,total) VALUES (?,?,?,?,?,?,?)");
		
                $idclients = $park->getidClients();
                $stmt->bindParam(1, $idclients );
		
		$idcar = $park->getidCar();
                $stmt->bindParam(2, $idcar);
		
                $datein= $park->getDatein();
                $stmt->bindParam(3, $datein);
                
                $dateout=$park->getDateout();

                $stmt->bindParam(4,$dateout);
               
                $status= $park->getStatus();
		$stmt->bindParam(5, $status);
                
                $fare= $park->getFare();
		$stmt->bindParam(6, $fare);
                
                $total= $park->getTotal();
                $stmt->bindParam(7, $total);
                
                $result=$stmt->execute();
                
               if($result){
                   return true;
               }else return 0;
               
	}
	
	public function update(ContactPark $park) {
		$db = $this->db;
		$id=$park->getId();
		$stmt = $db->prepare("update park p SET
                    p.clients=? ,p.car =?,p.datein=?,p.dateout=? ,p.status=? ,p.fare=? ,p.hours=? ,p.total=?WHERE P.id=$id");
		
                 $idclients = $park->getidClients();
                $stmt->bindParam(1, $idclients );
		
		$idcar = $park->getidCar();
                $stmt->bindParam(2, $idcar);
                
                $datein= $park->getDatein();
                $stmt->bindParam(3, $datein);
                
                $dateout= $park->getDateout();
                $stmt->bindParam(4,$dateout);
               
                $status= $park->getStatus();
		$stmt->bindParam(5, $status);
                
                $fare= $park->getFare();
		$stmt->bindParam(6, $fare);
                
                $hours= $park->getHours();
		$stmt->bindParam(7, $hours);
                
                $total= $park->getTotal();
                $stmt->bindParam(8, $total);
                
                $result=$stmt->execute();
               
                
               if($result){
                   return true;
               }else return 0;
               
	}
	
	public function remove($id) {
		header("Content-Type: text/plain");
		echo "The remove method was called. ID: " . $id;
		exit;
	}
	
	public function getById($id) {
           
		$park = null;
		$db = $this->db;
		
		$stmt = $db->prepare("select
                    p.id,
                    p.clients,
                    p.car,
                    c.name,
                    car.mark,
                    c.cpf,
                    car.model, 
                    car.plaque,
                    car.year,
                    p.datein,
                    p.dateout,
                    p.fare FROM park p join clients c on c.id = p.clients join car on car.id = p.car where p.id = ? and  p.status =0");
		$stmt->bindParam(1, $id);
		$stmt->execute();
		
		$row = $stmt->fetch();
		
		if(isset($row['id'])) {
			$park = new ContactPark();
                        $park->setId($row['id']);
                        $park->setClients($row['name']);
                        $park->setIdClients($row['clients']);
                        $park->setcpf($row['cpf']);
                        $park->setModel($row['model']);
                        $park->setIdCar($row['car']);
                        $park->setMark($row['mark']);
                        $park->setYear($row['year']);
                        $park->setplaque($row['plaque']);
                        $park->setDatein($row['datein']);
                        $park->setDateout($row['dateout']);
                        $park->setFare($row['fare']);
                        
                        
		}
		
		return $park;
	}
        
        
      
        
        
        
	
	public function getList() {
		$contactpark = array();
		$db = $this->db;
		
               
                $stmt = $db->prepare("select p.fare,p.id,c.name,car.mark, c.cpf,car.model, car.plaque, car.year,p.datein,p.dateout ,p.total,p.status FROM park p join clients c on c.id = p.clients join car on car.id = p.car where p.status = '1'");
		$stmt->execute();
		$result = $stmt->fetchAll();
                
                
		if(sizeof($result) > 0) {
			foreach($result as $row) {
				
			
                        $park = new ContactPark();
                        $park->setId($row['id']);
                        $park->setClients($row['name']);
                        $park->setcpf($row['cpf']);
                        $park->setModel($row['model']);
                        $park->setMark($row['mark']);
                        $park->setYear($row['year']);
                        $park->setplaque($row['plaque']);
                        $datein=$this->convertsDateIn($row['datein']);
                        $park->setDatein($datein);
                        $dateout=$this->convertsDateOut($row['dateout']);
                        $park->setDateout($dateout);
                        $park->setTotal($row['total']);
                        
                        $park->setSatatus($row['status']);
                        $park->setFare($row['fare']);
                        
                        $contactpark[]=$park;
                
                
		}
		
		
	} 
    return $contactpark;
}



public function getListOpen() {
		$contactpark = array();
		$db = $this->db;
		
               
                $stmt = $db->prepare("select p.id,c.name,car.mark, c.cpf,car.model, car.plaque, car.year,p.datein,p.dateout ,p.total FROM park p join clients c on c.id = p.clients join car on car.id = p.car and p.status='0'");
		$stmt->execute();
		$result = $stmt->fetchAll();
                
                
		if(sizeof($result) > 0) {
			foreach($result as $row) {
				
			
                        $park = new ContactPark();
                        $park->setId($row['id']);
                        $park->setClients($row['name']);
                        $park->setcpf($row['cpf']);
                        $park->setModel($row['model']);
                        $park->setMark($row['mark']);
                        $park->setYear($row['year']);
                        $park->setplaque($row['plaque']);
                        $datein=$this->convertsDateIn($row['datein']);
                        $park->setDatein($datein);
                        $dateout=$this->convertsDateOut($row['dateout']);
                        $park->setDateout($dateout);
                        $park->setTotal($row['total']);
                        
                        $contactpark[]=$park;
                
                
		}
		
		
	} 
    return $contactpark;
}





public function convertsDateIn($data){


        if ( ! strstr( $data, '/' ) )
        {
                // $data est� no formato ISO (yyyy-mm-dd) e deve ser convertida
                // para dd/mm/yyyy
                sscanf( $data, '%d-%d-%d %d:%d:%d', $y, $m, $d ,$H,$i,$s);
                return sprintf( '%d/%d/%d %d:%d:%d', $d, $m, $y ,$H,$i,$s );
        }
        else
        {
                // $data est� no formato brasileiro e deve ser convertida para ISO
                sscanf( $data, '%d-%d-%d %d:%d:%d', $d, $m, $y,$H,$i,$s );
                return sprintf( '%d-%d-%d %d:%d:%d', $y, $m, $d,$H,$i,$s );
        }
 
        return false;
}

 
    
    


public function convertsDateOut($data){


        if ( ! strstr( $data, '/' ) )
        {
                // $data est� no formato ISO (yyyy-mm-dd) e deve ser convertida
                // para dd/mm/yyyy
                sscanf( $data, '%d-%d-%d %d:%d:%d', $y, $m, $d ,$H,$i,$s);
                return sprintf( '%d/%d/%d %d:%d:%d', $d, $m, $y ,$H,$i,$s );
        }
        else
        {
                // $data est� no formato brasileiro e deve ser convertida para ISO
                sscanf( $data, '%d-%d-%d %d:%d:%d', $d, $m, $y,$H,$i,$s );
                return sprintf( '%d-%d-%d %d:%d:%d', $y, $m, $d,$H,$i,$s );
        }
 
        return false;


 
    
    
}}

?>
